草庐IT

PHPUnit 模拟 View 助手 ZF2

全部标签

ruby - RSpec 模拟 :each block

我想使用RSpec模拟来为block提供固定输入。ruby:classParserattr_accessor:extracteddefparse(fname)File.open(fname).eachdo|line|extracted=lineifline=~/^RCSfile:(.*),v$/endendendR规范:describeParserbeforedo@parser=Parser.new@lines=mock("lines")@lines.stub!(:each)File.stub!(:open).and_return(@lines)endit"shouldextracta

ruby-on-rails - 如何使用 block 创建助手?

我想做一个像下面这样的助手。defmy_divsome_options,&block#HowdoIprinttheresultoftheblock?end 最佳答案 你应该使用CaptureHelper.defmy_div(some_options,&block)#capturethevalueoftheblockastringcontent=capture(&block)#concatthevaluetotheoutputconcat(content)endThecontentdefmy_div(some_options,&blo

ruby-on-rails - 订购 .each 结果在 View 中

我想知道是否可以在View中指定顺序(即:order=>'created_atDESC')。我意识到View中的逻辑并不理想,但我似乎在定位影响此输出的位置时遇到了一些问题。例如,这是我的代码:CreatedaboutagoUpdatedaboutago|'Areyousure?',:method=>:delete%>在我的QuestionsController中,我有以下索引操作,但它不会影响上面代码的输出。classQuestionsController'created_atDESC',:limit=>20)respond_todo|format|format.html#index

ruby - 将局部变量从 Controller 传递给 View

由于某些原因,我无法将局部变量传递给显示View...在我的Controller中,我只是:defshowrendertemplate:"books/show",:resource=>"Sometext"end在我看来,我打印了以下内容:Mylocalvariabletext:我收到以下消息:undefinedlocalvariableormethod`resource'for#:0x00000118ec3498>我在Controller中尝试了以下语法:rendertemplate:"books/show",locals:{resource:"Sometext"}rendertemp

ruby - 如何在 Rails 4 中访问 *_path 和 *_url 助手

我正在阅读Rails:Checkoutputofpathhelperfromconsole并且没有一个解决方案对我有用,大概是因为它们都适用于Rails2/3。在Rails4/5中,如何从Rails控制台访问*_path和*_url助手? 最佳答案 运行Rails4,我得到它们:app.root_path=>"/"app.users_url=>"http://www.example.com/users" 关于ruby-如何在Rails4中访问*_path和*_url助手,我们在Stack

ruby-on-rails - 如何覆盖 Rails 中的 View

上下文正如其他人在2012年之前询问的那样,请参阅下面的链接,我想知道我的RailsView是否包含在我的规范/cuc中。我知道在2012年的答案是没有解决方案。我想知道2014年现在是否有解决方案?我在网上搜索并没有找到太多,所以我担心答案是一样的。动机我想知道我的Cucumber特性是否涵盖了API的所有部分(在本例中是GUI)。当我添加GUI功能(新View、按钮等)但不知何故忘记为其添加cuccie时,我想收到通知。所以与其说我想涵盖我的观点中的所有内容,不如说我想防止我无意中忘记为新用户功能添加一个cuccie。因此减少了代码在生产中不起作用的可能性。研究我找到了上面提到的以

ruby - python -i 的 IRB 模拟

我想使用IRB运行脚本然后给我一个交互式提示。我在Python中使用python-ixy.py执行此操作,但是irbxy.rb在执行后退出。>python--help-iWhenascriptispassedasfirstargumentorthe-coptionisused,enterinteractivemodeafterexecutingthescriptorthecommand 最佳答案 irb-rxy.rb它只需要在给你一个正常的IRB提示之前提到的文件。 关于ruby-pyt

ruby - 模拟默认对象#inspect 输出?

o=Object.newo.instance_eval{@str="foo"}po#=>#这很好。使用对象作为参数调用p会打印对象inspect方法的输出。但是,不幸的是,如果对象重写了to_s方法,那么它将输出该方法的输出:class"foo"po#=>foo所以要解决这个问题,我们必须在我们的对象上定义一个inspect方法:class"blah"如何使对象的inspect方法输出默认的Ruby方式,如第一个代码示例的第3行所示?我最接近的是下面的,但我不确定它是否完全正确class"endend 最佳答案 为了使数字与原来的实

ruby - 你如何在 Rspec 中模拟/ stub 反引号系统调用?

我有一个使用反引号的方法,但stub不适用于它。它正在拉取ls的结果:classTestHelperdefself.test_method`ls`endendrspec测试:describeTestHelperdodescribe'.test_method'dosubject{described_class.test_method}before{Kernel.stub(:`).and_return("test_dir")}it{expect(subject).toeql("test_dir")}endend 最佳答案 TestHel

ruby-on-rails - 如何在 Rails 助手中将内容包装在 html 中

我想在Rails3助手中用HTML包装一些内容,这样在我看来我可以这样做:我有一个如下所示的辅助方法:defrounded_box(&block)str="str"rawstrend我现在使用它的方式返回正确包装在HTML字符串中的内容,但不会在呈现rounded_boxblock中的任何erb之前返回(例如,在这种情况下,target.text呈现两次,一次包装,一次不包装)。有更好的方法吗?为简单起见,我想避免使用content_tag,但如果这是我能做到的唯一/最佳方式。 最佳答案 在block上调用capture而不是yie